package com.lbssoft.gpsbd.controller;

import com.lbssoft.datalink.util.Log;
import com.lbssoft.gpsbd.annotation.LogAnnotation;
import com.lbssoft.gpsbd.common.Utils;
import com.lbssoft.gpsbd.controller.base.BaseController;
import com.lbssoft.gpsbd.model.Layer;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.LayerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api("自定义图层")
@RestController
@RequestMapping("/layers")
public class LayerController extends BaseController {


	@Autowired
	private LayerService layerService;

	@ApiOperation(value="获取自定义图层", notes="获取自定义图层")
	@RequiresPermissions("user:layer:view")
	@RequestMapping(value = "",method = {RequestMethod.GET})
	@ResponseBody
	public Result list() {
		UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
		Result result = new Result();
		List<Layer> list = layerService.list(user.getId());
		result.setData(list);
		return result;
	}

	@ApiOperation(value="修改图层信息", notes="修改图层信息")
	@ApiImplicitParam(paramType="body",name = "layer", value = "修改图层信息", dataType = "Layer")
	@LogAnnotation(targetType = "layer",action = "update",remark = "修改图层信息")
	@RequiresPermissions("user:layer:update")
	@RequestMapping(value = "",method = {RequestMethod.PUT})
	@ResponseBody
	public Result update(@Validated @RequestBody Layer layer) {
		Result result = new Result();
		layerService.update(layer);
		return result;
	}

	@ApiOperation(value="创建图层", notes="创建图层")
	@ApiImplicitParam(paramType="body",name = "layer", value = "创建图层", dataType = "Layer")
	@LogAnnotation(targetType = "layer",action = "add",remark = "创建图层")
	@RequiresPermissions("user:layer:add")
	@RequestMapping(value = "",method = {RequestMethod.POST})
	@ResponseBody
	public Result add(@Validated @RequestBody Layer layer) {
		Result result = new Result();
		Log.info(layer.getUrl());
		Log.info("url:"+Utils.isHttpImgUrl(layer.getUrl()));
		if(!Utils.isHttpImgUrl(layer.getUrl())){
			result.setCode(1);
			result.setMsg("图层格式只能是png、jpg格式");
			return result;
		}
		layerService.add(layer);
		return result;
	}

	@ApiOperation(value="删除图层", notes="删除图层")
	@ApiImplicitParam(paramType="path",name = "id", value = "图层id", required = true, dataType = "int")
	@LogAnnotation(targetType = "layer",action = "delete",remark = "删除图层")
	@RequiresPermissions("user:layer:delete")
	@RequestMapping(value = "/{id}",method = {RequestMethod.DELETE})
	@ResponseBody
	public Result delete(@NotNull @PathVariable int id) {
		Result result = new Result();
		layerService.delete(id);
		return result;
	}
}